package pers.jing.android.helper.page

import androidhelper.composeapp.generated.resources.Res
import androidhelper.composeapp.generated.resources.back
import androidhelper.composeapp.generated.resources.settings
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel
import pers.jing.android.helper.model.STATE_FAILURE
import pers.jing.android.helper.model.STATE_SUCCESS
import pers.jing.android.helper.widget.ConfirmDialog
import pers.jing.android.helper.widget.ProgressDialog
import pers.jing.android.helper.widget.normalAppBarColors

/**
 * 使用viewModel{ xx() }创建的ViewModel, 每次页面显示都会创建一次
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsPage(viewModel: SettingsViewModel = koinViewModel<SettingsViewModel>(), onClickBack: () -> Unit) {
    val scrollState = rememberScrollState()
    viewModel.loadConfig()
    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(text = stringResource(Res.string.settings))
                },
                navigationIcon = {
                    IconButton(
                        onClick = {
                            onClickBack()
                        }
                    ) {
                        Icon(imageVector = Icons.AutoMirrored.Rounded.ArrowBack, contentDescription = stringResource(Res.string.back))
                    }
                },
                colors = TopAppBarDefaults.normalAppBarColors()
            )
        },
    ) { paddings ->
        Column(
            modifier = Modifier.fillMaxSize()
                .padding(paddings)
                .verticalScroll(scrollState),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            var androidSdkPath = viewModel.androidSdkPath.collectAsState()
            val uiResultState = viewModel.result.collectAsState()
            val uiProgressState = viewModel.progress.collectAsState()
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.padding(top = 10.dp)
                    .padding(horizontal = 16.dp)
            ) {
                OutlinedTextField(
                    value = androidSdkPath.value,
                    onValueChange = {
                        viewModel.updateAndroidSdkPath(it)
                    },
                    label = {
                        Text("Android Sdk路径")
                    },
                    placeholder = {
                        Text("~/Android/Sdk")
                    },
                    maxLines = 1,
                    modifier = Modifier.weight(1.0f)
                )

                Button(
                    enabled = !androidSdkPath.value.isEmpty(),
                    onClick = {
                        viewModel.install(androidSdkPath.value)
                    }, modifier = Modifier.padding(start = 10.dp)) {
                    Text("安装")
                }
            }

            HorizontalDivider(
                modifier = Modifier.height(1.dp).fillMaxWidth().padding(top = 10.dp)
            )

            Row(
                modifier = Modifier.fillMaxWidth().padding(top = 10.dp),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                Button(
                    onClick = {
                        viewModel.addApkContextMenu()
                    }
                ) {
                    Text("安装右击菜单“安装APK”")
                }

                Button(
                    onClick = {
                        viewModel.deleteApkContextMenu()
                    }
                ) {
                    Text("卸载右击菜单“安装APK”")
                }
            }

            if (STATE_FAILURE == uiResultState.value.state || STATE_SUCCESS == uiResultState.value.state) {
                ConfirmDialog(uiResultState.value.msg?:"unknown", onConfirm = {
                    viewModel.clearResult()
                }, {
                    viewModel.clearResult()
                })
            }
            if (uiProgressState.value.show) {
                ProgressDialog(text = "正在安装")
            }
        }
    }

}